package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.ApplicationContext;
import com.itheima.mapper.ShoppingCartDao;
import com.itheima.pojo.ShoppingCart;
import com.itheima.result.R;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: 斌
 * @DateTime: 2023年02月10日  21:15:20
 * @Description: com.itheima.service.Impl
 * @Version: 1.0
 */
@Slf4j
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ShoppingCartDao shoppingCartDao;
    @Override
    public R shopCartList(HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null,ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartDao.selectList(wrapper);
        return R.success(shoppingCarts);
    }

    @Override
    public R addCart(ShoppingCart shoppingCart,HttpSession session) {
        //主要思路，判断购物车中有误菜品或套餐，没，就往数据库中添加，有，就执行添加操作(用户添加一次，查一次，添加/修改一次)
        //确定当前操作的用户身份
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(userId != null,ShoppingCart::getUserId,userId);
        //判断菜品Id/套餐Id是否为null,进行条件的拼接，然后进行查询，主要是为了在添加某道菜品时，查询其是否被添加过
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            qw.eq(ShoppingCart::getDishId,dishId);
        }else {
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartDao.selectOne(qw);
        //判断查询结果是否为null，
        if (one != null){
            //不为null，修改菜品数量，修改时，在从数据库中获取数量的基础上加一，因为你添加一次，就要走一遍流程
            one.setNumber(one.getNumber() + 1);
            shoppingCartDao.updateById(one);

        }else {
            //为null，进行添加，置----1
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartDao.insert(shoppingCart);
        }


        return R.success("添加成功");
    }

    /**
     * 移除购物车数据
     * @param shoppingCart
     * @param session
     * @return
     */

    @Override
    public R removeCart(ShoppingCart shoppingCart, HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(userId != null,ShoppingCart::getUserId,userId);
        //判断菜品Id/套餐Id是否为null,进行条件的拼接，然后进行查询，主要是为了在添加某道菜品时，查询其是否被添加过
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            qw.eq(ShoppingCart::getDishId,dishId);
        }else {
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartDao.selectOne(qw);
        //判断查询结果是否为null，
        if (one.getNumber() > 1){
            //不为null，修改菜品数量，修改时，在从数据库中获取数量的基础上加一，因为你添加一次，就要走一遍流程
            one.setNumber(one.getNumber() - 1);
            shoppingCartDao.updateById(one);

        }else {
            //等于，直接移除该条数据
             shoppingCartDao.delete(qw);
        }
        return R.success(one);

    }

    @Override
    public R deleteCart() {
        Long userId = (Long) ApplicationContext.getData();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null,ShoppingCart::getUserId,userId);
        int deleteResult = shoppingCartDao.delete(wrapper);
        return deleteResult > 0 ? R.success("清空购物车成功") : R.error("清空购物车失败");
    }
}
